package test;

import java.awt.Color;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URI;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

public class PolicyWebsiteService extends JFrame {
    public PolicyWebsiteService() {
        this.setTitle("政策服务网站");
        this.setSize(800, 600);
        this.setDefaultCloseOperation(2);
        this.setLocationRelativeTo((Component)null);
        JPanel mainPanel = new JPanel(new GridLayout(0, 3, 10, 10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        this.addTopicButton(mainPanel, "法律", "https://www.example.com/legal");
        this.addTopicButton(mainPanel, "环保", "https://www.example.com/environment");
        this.addTopicButton(mainPanel, "教育", "https://www.example.com/education");
        this.addTopicButton(mainPanel, "医疗", "https://www.example.com/medical");
        this.addTopicButton(mainPanel, "交通", "https://www.example.com/transportation");
        this.addTopicButton(mainPanel, "就业", "https://www.example.com/employment");
        this.addTopicButton(mainPanel, "农业", "https://www.example.com/agriculture");
        this.addTopicButton(mainPanel, "科技", "https://www.example.com/technology");
        this.addTopicButton(mainPanel, "文化", "https://www.example.com/culture");
        this.add(new JScrollPane(mainPanel), "Center");
        JPanel bottomPanel = new JPanel(new FlowLayout(2));
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MannageService.main((String[])null);
                PolicyWebsiteService.this.dispose();
            }
        });
        bottomPanel.add(backButton);
        this.add(bottomPanel, "South");
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent windowEvent) {
                MannageService.main((String[])null);
            }
        });
    }

    private void addTopicButton(JPanel panel, String topicName, String url) {
        JButton button = new JButton(topicName);
        button.setPreferredSize(new Dimension(200, 100));
        button.setFont(new Font("SimSun", 1, 16));
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setBackground(Color.WHITE);
        button.addActionListener((e) -> {
            try {
                Desktop.getDesktop().browse(URI.create(url));
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        });
        panel.add(button);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            PolicyWebsiteService policyWebsiteService = new PolicyWebsiteService();
            policyWebsiteService.setVisible(true);
        });
    }
}